package cn.wolfcode.wolf2world.web.controller;

import cn.wolfcode.wolf2world.domain.UserInfo;
import cn.wolfcode.wolf2world.domain.UserStrategyFavoer;
import cn.wolfcode.wolf2world.redis.service.StaticRedisService;
import cn.wolfcode.wolf2world.redis.service.UserRedisService;
import cn.wolfcode.wolf2world.service.UserInfoService;
import cn.wolfcode.wolf2world.vo.JsonResult;
import cn.wolfcode.wolf2world.web.annotation.RequiredLogin;
import cn.wolfcode.wolf2world.web.annotation.UserParam;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 杨兴健
 * @Date 2020/8/7 14:30
 */
@Api()
@RestController
@RequestMapping("/users")
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private UserRedisService userRedisService;

    @ApiOperation("使用用户id获取用户信息的api")
    @GetMapping("/get")
    public Object get(String id) {
        return userInfoService.get(id);
    }

    /**
     * 检查手机号是否存在
     * @param phone 手机号
     * @return false 不可以注册  ,true 可以注册
     */
    @GetMapping("/checkPhone")
    public Object checkPhone(String phone) {
        boolean flog = userInfoService.checkPhone(phone);
        return !flog;
    }

    /**
     * 发送短信验证码
     * @param phone 手机号
     * @return .
     */
    @GetMapping("/sendVerifyCode")
    public Object sendVerifyCode(String phone) {
        userInfoService.sendVerifyCode(phone);
        return JsonResult.success();
    }

    /**
     * 注册用户
     * @return .
     */
    @PostMapping("/regist")
    public Object regist(String phone,String nickname,String password,String rpassword,String verifyCode) {
        userInfoService.regist(phone,nickname,password,rpassword,verifyCode);
        return JsonResult.success();
    }

    /**
     * 用户登录
     * @param username 手机号码
     * @param password 密码
     * @return .
     */
    @PostMapping("/login")
    public JsonResult login(String username,String password) {
        //用户登录
        UserInfo user = userInfoService.login(username,password);
        //保存当前用户在redis缓存数据库中
        String token = userRedisService.saveUser(user);
        //封装user和token
        Map<String, Object> map = new HashMap<>();
        map.put("token", token);
        map.put("user", user);
        return JsonResult.success(map);
    }

    /**
     * 获取当前登录用户
     * @return
     */
    @RequiredLogin
    @GetMapping("/currentUser")
    public JsonResult currentUser(@UserParam UserInfo userInfo) {
        return JsonResult.success(userInfo);
    }
    /**
     * 获取当前登录用户
     * @return
     */
    @RequiredLogin
    @GetMapping("/requestUser")
    public JsonResult requestUser(UserInfo userInfo) {
        return JsonResult.success(userInfo);
    }

    /**
     * 获取用户收藏的攻略
     * @param
     * @return
     */
    @GetMapping("/strategies/favor")
    public JsonResult userFavor(@UserParam UserInfo userInfo) {
        UserStrategyFavoer usf = userRedisService.getUserStrategyFavor(userInfo.getId());
        return JsonResult.success(usf != null ? usf.getSid() : null);
    }


    /**
     * 统计访问数
     * @return
     */
    @RequiredLogin
    @GetMapping("/statcview")
    public Object statcview(String id,@UserParam UserInfo userInfo) {
        Map map = userRedisService.statcview(id,userInfo.getId());
        UserInfo user = userInfoService.get(id);
        map.put("owner",user);
        return JsonResult.success(map);
    }

    /**
     * 进入窝
     * @return
     */
    @GetMapping("/mywo")
    public Object mywo() {

        return null;
    }




}
